import { imapi } from './instance'
import { prefix } from './js/config'
export default {
	//群组创建自带前缀，其他地方群组id没有自带前缀需要获取im配置文件前缀
	async createGroup({
		groupID = '',
		groupName = '',
		groupAvatarUrl = '',
		userIDs,
		groupNotice = '',
		groupAttributes = {}
	}) {
		// groupID 最大 32 字节的字符串。仅支持数字，英文字符 和 '!', '#', '$', '%', '&', '(', ')', '+', '-', ':', ';', '<', '=', '.', '>', '?', '@', '[', ']', '^', '_', '{', '}', '|', '~'，且不能以 ’#‘ 开头。
		// groupName 最大 50 字节的字符串，无特殊字符限制。
		const groupInfo = { groupID: prefix + groupID.toString(), groupName, groupAvatarUrl };
		if (!Array.isArray(userIDs)) userIDs = [userIDs]
		const config = {
			groupNotice,
			groupAttributes
		};
		const data = await imapi({ name: 'createGroup', attr: 'groupInfo' }, groupInfo, userIDs, config)
		return data
	},
	//直接加入群组
	async joinGroup(groupID) {
		const data = await imapi({ name: 'joinGroup' }, groupID)
		return data
	},
	//由群内成员添加入群组
	async inviteUsersIntoGroup({ groupID = '', userIDs }) {
		if (!Array.isArray(userIDs)) userIDs = [userIDs]
		const data = await imapi({ name: 'inviteUsersIntoGroup' }, userIDs, groupID)
		return data
	},
	//修改群组名称
	async updateGroupName({ groupID = '', groupName }) {
		const data = await imapi({ name: 'updateGroupName' }, groupName, groupID)
		return data
	},
	//修改群组头像
	async updateGroupAvatarUrl({ groupID = '', groupAvatarUrl }) {
		const data = await imapi({ name: 'updateGroupAvatarUrl' }, groupAvatarUrl, groupID)
		return data
	},
	//解散群组
	async dismissGroup(groupID) {
		const data = await imapi({ name: 'dismissGroup' }, groupID)
		return data
	},
	//主动退出群组
	async leaveGroup(groupID) {
		const data = await imapi({ name: 'leaveGroup' }, groupID)
		return data
	},
	//群主移除群内成员
	async kickGroupMembers({ groupID = '', userIDs }) {
		if (!Array.isArray(userIDs)) userIDs = [userIDs]
		const data = await imapi({ name: 'kickGroupMembers' }, userIDs, groupID)
		return data
	},
	//群成员列表
	async queryGroupMemberList(groupID) {
		const config = { count: 100, nextFlag: 0 };
		const data = await imapi({ name: 'queryGroupMemberList', attr: 'userList' }, groupID, config)
		if (data.code === 0) {
			data.data.forEach(v => {
				v.baseInfo = {
					userID: v.userID,
					userAvatarUrl: v.userAvatarUrl,
					userName: v.userName
				}
			})
			uni.setStorageSync('imUserInfo', data.data)
		}
		return data
	},
	//获取群信息
	async queryGroupInfo(groupID) {
		const data = await imapi({ name: 'queryGroupInfo', attr: 'groupInfo' }, groupID)
		return data
	}
}